// 错题操作API
import { NextRequest } from 'next/server';
import { MistakeDB } from '@/lib/mysql';
import { getCurrentUser, ApiResponse } from '@/lib/auth';

// 标记错题为已复习
export async function PATCH(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    // 验证用户登录
    const currentUser = getCurrentUser();
    if (!currentUser) {
      return ApiResponse.unauthorized('请先登录');
    }

    const mistakeId = parseInt(params.id);
    if (isNaN(mistakeId)) {
      return ApiResponse.error('无效的错题ID');
    }

    // 解析请求体
    const body = await request.json();
    const { action } = body;

    if (action === 'review') {
      // 标记为已复习
      await MistakeDB.markReviewed(mistakeId);
      return ApiResponse.success(null, '已标记为已复习');
    } else if (action === 'master') {
      // 标记为已掌握
      await MistakeDB.markMastered(mistakeId);
      return ApiResponse.success(null, '已标记为已掌握');
    } else {
      return ApiResponse.error('无效的操作');
    }
  } catch (error) {
    console.error('更新错题状态错误:', error);
    return ApiResponse.serverError('更新错题状态失败');
  }
}
